﻿<div ng-controller="DepotCommodityListC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">{{typename1}}商品管理---{{$stateParams.Name}}</h1>
    </div>
    <div class="wrapper-md" style="text-align: center;">
        <div class="panel panel-default">
            <div class="panel-heading" style="overflow:auto">
                <div class=" col-lg-3 col-md-3 col-sm-3 col-xs-12" style="display: flex;">
                    <input type="text" class="form-control" placeholder="按名称查找" ng-model="findname">
                    <!--<span class="input-group-btn">-->
                    <button class="btn btn-default" type="button" ng-click="find()">查找</button>
                    <!--</span>-->
                </div>
                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                    <div ng-if="!istrue" class="input-group">
                        <input class="form-control" ng-model="editnum" type="number">
                        <span class="input-group-btn">
                            <button class="btn btn-default" type="button" ng-click="editnumFun(editnum)">批量调价</button>
                        </span>
                    </div>
                </div>
                <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 text-right" style="float: right">
                    <button ng-if="$stateParams.type == 0" class="btn btn-sm btn-default" ng-click="$state.go('app.Provider_AddDepotCommodity', { 'id': $stateParams.id, 'sid':$stateParams.sid , 'Name':$stateParams.Name, 'type':$stateParams.type})">
                        <i class="fa fa-plus text"></i>
                        <span class="text">添加</span>
                    </button>
                    <button ng-if="istrue" class="btn btn-sm btn-default" ng-click="goadd(0)">
                        <i class="fa fa-plus text"></i>
                        <span class="text">修改价格</span>
                    </button>
                    <button ng-if="!istrue" class="btn btn-sm btn-default" ng-click="gosub(0)">
                        <i class="fa fa-plus text"></i>
                        <span class="text">提交改价申请</span>
                    </button>
                    <button ng-if="istrue1&&$stateParams.type==0" class="btn btn-sm btn-default" ng-click="goadd(1)">
                        <i class="fa fa-plus text"></i>
                        <span class="text">修改库存</span>
                    </button>
                    <button ng-if="!istrue1&&$stateParams.type==0" class="btn btn-sm btn-default" ng-click="gosub(1)">
                        <i class="fa fa-plus text"></i>
                        <span class="text">保存库存</span>
                    </button>
                    <button type="button" class="btn btn-default" ng-click="goorderList()">返回</button>
                </div>


                <!--<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">-->
                    <!--<div class="input-group">-->
                        <!--<input type="text" class="form-control" placeholder="按名称查找" ng-model="findname">-->
                        <!--<span class="input-group-btn">-->
                            <!--<button class="btn btn-default" type="button" ng-click="find()">查找</button>-->
                        <!--</span>-->
                    <!--</div>-->
                <!--</div>-->
            </div>
            <div class="table-responsive">
                <table id="example1" class="table table-striped b-t b-light table-bordered">
                    <thead>
                        <tr>
                            <th style="text-align: center;">名称</th>
                            <th style="text-align: center;">销售单价</th>
                            <th style="text-align: center;">库存（t）</th>
                            <th style="text-align: center;">锁定重量（t）</th>
                            <th style="text-align: center;">锁定数量</th>
                            <!--<th style="text-align: center;">允许最大负库存（t）</th>-->

                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="item in pageData">
                            <td style="width: 25%">{{item.Commodity.Supplier.Brand}}/{{item.Commodity.Name}}/{{item.Commodity.Format}}/{{item.Commodity.Standard}}</td>
                            <td style="width: 15%"><input ng-disabled="istrue" ng-model="item.Price" type="number" min="0" class="form-control" required placeholder="请输入商品单价"></td>
                            <td ng-if="$stateParams.type != 0" style="width: 15%">{{item.Amount | number:3}}</td>
                            <td ng-if="$stateParams.type == 0" style="width: 15%"><input ng-disabled="istrue1" ng-model="item.Amount" type="number" min="0" class="form-control" required placeholder="请输入商品库存"></td>
                            <td style="width: 15%">{{item.LockAmount | number:3}}</td>
                            <td style="width: 15%">{{item.LockCount}}</td>
                            <!--<td style="width: 15%"><input ng-disabled="istrue" ng-model="item.MinAmount" type="number" min="0" class="form-control" required placeholder="请输入允许最大负库存"></td>-->

                            <!--<td style="width: 20%">-->
                            <!--<button class="btn btn-sm btn-default" ng-click="commedit()">-->
                            <!--<i class="fa fa-plus text"></i>-->
                            <!--<span class="text">编辑</span>-->
                            <!--</button>-->
                            <!--</td>-->
                        </tr>
                    </tbody>
                </table>
                <!--<div class="panel-heading" ng-include="'/htm/tpl/Supplier/Paging.html'"></div>-->
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')//仓库商品列表
            .controller("DepotCommodityListC", function ($scope, $state, $http, $stateParams) {
                $scope.pageData = {};
                $scope.editnum = 0;
                $scope.pageData.Page = 1;
                $scope.findname = "";
                if($stateParams.type == 0){
                    $scope.typename1 = "钢厂";
                }else if($stateParams.type == 1){
                    $scope.typename1 = "仓库";
                } else if ($stateParams.type == 2) {
                    $scope.typename1 = "贸易商";
                }
                if($stateParams.num == 0){
                    $scope.myurl2 = 'app.TraderManage_List';
                } else if ($stateParams.num == 1) {
                    $scope.myurl2 = 'app.Warehouse_List';
                } else {
                    $scope.myurl2 = 'app.Provider_List';
                }
//                $scope.newObj = "";

                $scope.pageData.Rows = $scope.defPageRows;
                $scope.istrue = true;
                $scope.istrue1 = true;
                //                $scope.commedit = function(){
                //                    $state.go('app.Provider_Edit', { 'id': $stateParams.id })
                //                };
                $scope.getData = function (p, r) {
                    if (p) {
                        p = p < 1 ? 1 : p;
                    } else p = 1;
                    if (r) {
                    } else r = $scope.defPageRows;
                    $http({
                        url: "/api/Provider/DepotCommodity",
                        method: "GET",
                        params: { depotId: $stateParams.id, name: $scope.findname, page: 1, rows: 999 }
                    })
                            .then(function (res) {
                                $scope.pageData = angular.copy(res.data);
                                $scope.pageData2 = angular.copy(res.data);
                                console.log($scope.pageData);
                            })
                            .then(function (error) {
                                if (error) {
                                    alert("数据获取失败");
                                }
                            });
                };
                $scope.getData(1, $scope.pageData.Rows);
                $scope.find = function () {
                    $scope.getData(1, $scope.pageData.Rows);
                };
                $scope.goadd = function (item) {
                    if(item == 0){
                        $scope.istrue = !$scope.istrue;
                        $scope.istrue1 = true;
                        $scope.getData(1, $scope.pageData.Rows);
                    }else if(item == 1){
                        $scope.istrue1 = !$scope.istrue1;
                        $scope.istrue = true;
                        $scope.getData(1, $scope.pageData.Rows);
                    }

                };
                $scope.gosub = function (item) {
                    if(item == 0){
                        $scope.myurl = "../api/Provider/PriceConfirmApproval";
                    }else if(item == 1){
                        $scope.myurl = "../api/Provider/EditProviderCommodity";
                    }
                    $scope.ischange = false;
//                    console.log($scope.pageData);
                    $scope.newObj = angular.copy($scope.pageData);
                    for (var i=0;i<$scope.newObj.length;i++)
                    {
                        if($scope.newObj[i].Price != $scope.pageData2[i].Price || $scope.newObj[i].Amount != $scope.pageData2[i].Amount ){
                           $scope.ischange = true;
                        }
                        delete $scope.newObj[i].Commodity;
                        delete $scope.newObj[i].Provider;
                    }
                    if ($scope.ischange) {
                        $http.post($scope.myurl, $scope.newObj)
                                .success(function (res) {
                                    if (item == 0) {
                                        if (res.State <= 0) {
                                            alert(res.Msg);
                                        } else {
                                            alert("已提交申请");
                                            $state.go('app.Provider_DepotCommodityList', { 'id': $stateParams.id, 'Name': $stateParams.Name, 'type': $stateParams.type });
                                        }
                                    } else if (item == 1) {
                                        alert("批量修改成功");
                                        $state.go('app.Provider_DepotCommodityList', { 'id': $stateParams.id, 'Name': $stateParams.Name, 'type': $stateParams.type });
                                    }
                                });
                    } else {
                        $scope.istrue = !$scope.istrue;
                        alert("您没有做更改！");
                        $scope.istrue = true;
                        $scope.istrue1 = true;
                    }
                };
                $scope.editnumFun = function(nub){
                    $scope.iseditnub = true;
                    console.log(nub);
                    if(nub<0 || nub>0){

                        if( window.confirm("确认统一调价"+nub+"?")){
                            for (var j=0;j<$scope.pageData.length;j++)
                            {
                                if($scope.pageData[j].Price+nub<=0){
                                    $scope.iseditnub = false;
                                }
                            }
                            if($scope.iseditnub){
                                for (var n=0;n<$scope.pageData.length;n++)
                                {
                                    $scope.pageData[n].Price+=nub;
                                }
                            }else{
                                alert("修改价格有误！")
                            }
                        }
                    }
                };
                //返回仓库列表
                $scope.goorderList = function () {
                    $state.go($scope.myurl2)
                };
            })

</script>
